package com.woniuxy.utils;
import java.util.Random;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/**
 * 以下所有功能原厂制造
 * @Author:吴毛伟
 */
public class PhoneCode {
    /**
     * @param phone
     *            发送到的手机号
     *
     * @param code
     *            验证码内容
     */
    public static void sendCode(String phone, String code) {
        //此处放AccessKeyID和AccessKeySecret
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GJ52gL6EKz2Rg1JAHYg","peBC2f5XTKTyhxKD56MPGQ7kXLWOgi");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        //此处放接收验证码的手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //此处放签名名称（必须审核通过）
        request.putQueryParameter("SignName", "健身App");
        //此处放短信模板（必须审核通过）
        request.putQueryParameter("TemplateCode", "SMS_193233990");
        //此处放验证码的内容（JSON格式\"表示转义。JSON格式：{"code":"666666"}）
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return 随机生成的6位验证码
     */
    public static String getCode() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; sb.length() < 6; i++) {
            int num = new Random().nextInt(10);
            sb.append(num);
        }
        return sb.toString();
    }
}
